<template>
	<div class="accessDivClass">
		<div v-show="ifshow">
			<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
				<el-form-item>
					<el-input v-model="state.queryForm.name" placeholder="名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="state.queryForm.type" placeholder="文件类型"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" :icon="Search" @click="getDataList()">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button v-auth="'data-integrate:file:save'" :icon="Plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
				</el-form-item>
				<el-form-item>
					<el-button v-auth="'data-integrate:file:delete'" :icon="Delete" type="danger" @click="deleteBatchHandle()">删除</el-button>
				</el-form-item>
			</el-form>
			<el-table
				v-loading="state.dataListLoading"
				:data="state.dataList"
				border
				style="width: 100%"
				max-height="calc(100vh - 340px )"
				@selection-change="selectionChangeHandle"
			>
				<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
				<el-table-column show-overflow-tooltip prop="name" label="名称" header-align="center" align="center"></el-table-column>
				<!-- <el-table-column show-overflow-tooltip prop="fileCategoryId" label="所属分组" header-align="center" align="center">{{
					state.path
				}}</el-table-column> -->
				<el-table-column prop="type" label="文件类型" header-align="center" align="center"></el-table-column>
				<el-table-column prop="standardNo" label="标准号" header-align="center" align="center"> </el-table-column>
				<fast-table-column
					prop="standardGroup"
					label="标准分类"
					header-align="center"
					align="center"
					dict-type="file_standard_type"
				></fast-table-column>
				<el-table-column prop="status" label="状态" header-align="center" align="center">
					<template #default="scope">
						{{ scope.row.status ? '现行' : '废止' }}
					</template>
				</el-table-column>
				<el-table-column prop="size" label="文件大小" header-align="center" align="center">
					<template #default="scope">
						{{ scope.row.size ? convertSizeFormat(scope.row.size) : '' }}
					</template>
				</el-table-column>
				<!-- <el-table-column type="expand" label="备注" width="80">
					<template #default="scope">
						<div style="margin-left: 20px">
							{{ scope.row.description ? scope.row.description : '无备注信息！' }}
						</div>
					</template>
				</el-table-column> -->
				<!-- <fast-creator-column prop="creator" label="创建者" header-align="center" align="center"></fast-creator-column> -->
				<el-table-column prop="creatUser" label="创建者" header-align="center" align="center"></el-table-column>
				<el-table-column prop="createTime" label="创建时间" header-align="center" align="center"></el-table-column>
				<el-table-column label="操作" fixed="right" header-align="center" align="center" width="180">
					<template #default="scope">
						<el-button type="primary" link @click="fileSee(scope.row.fileUrl, scope.row.type, scope.row.name, scope.row.id)">预览</el-button>
						<el-button type="primary" link @click="downFile(scope.row.fileUrl, scope.row.name, scope.row.id)">下载</el-button>
						<el-button v-auth="'data-integrate:file:delete'" type="primary" link @click="deleteBatchHandle(scope.row.id)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				:current-page="state.page"
				:page-sizes="state.pageSizes"
				:page-size="state.limit"
				:total="state.total"
				layout="total, sizes, prev, pager, next, jumper"
				@size-change="sizeChangeHandle"
				@current-change="currentChangeHandle"
			>
			</el-pagination>
			<div class="drawerClass" style="height: 100%">
				<iframe-file ref="iframeFileRef" @downFile="getDataList"></iframe-file>
			</div>
		</div>

		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList"></add-or-update>
	</div>
</template>

<script setup lang="ts" name="Data-integrateFileIndex">
import { Delete, Edit, Search, Plus, CircleClose, Refresh } from '@element-plus/icons-vue'
import { useCrud } from '@/hooks'
import { useCrudFiles } from '@/hooks/indexFilesPdf'
import { convertSizeFormat } from '@/utils/tool'
import { nextTick, reactive, ref } from 'vue'
import AddOrUpdate from './add-or-update.vue'
import { IHooksOptions } from '@/hooks/interface'
import IframeFile from '@/components/iframe-file/index.vue'
import { addIncreaseDownload } from '@/api/standard-services/index'

const state: IHooksOptions = reactive({
	createdIsNeed: false,
	projectId: '',
	path: '',
	fileCategoryId: '',
	dataListUrl: '/data-governance/standard-file/page',
	deleteUrl: '/data-governance/standard-file',
	queryForm: {
		name: '',
		fileCategoryId: '',
		type: ''
	}
})

const ifshow = ref(false)

/* 初始化文件分组id */
const init = (fileCategoryId: number, path: any, projectId: any) => {
	state.queryForm.fileCategoryId = fileCategoryId
	state.path = path
	state.fileCategoryId = fileCategoryId
	state.projectId = projectId
	getDataList()
	ifshow.value = true

	iframeFileRef.value.visible = false
}

const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
	addOrUpdateRef.value.init(id, state.fileCategoryId, state.path, state.projectId)
}

const iframeFileRef = ref()
const fileSee = (url: any, type: any, name, id) => {
	nextTick(() => {
		iframeFileRef.value.init(url, type, name, id)
	})
}

const downFile = (url, name, id) => {
	if (url) {
		downloadHandle(url, name)
		addIncreaseDownload(id).then(() => {
			getDataList()
		})
	}
}

const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle } = useCrud(state)
const { downloadHandle } = useCrudFiles({})
defineExpose({
	init
})
</script>
<style lang="scss">
.accessDivClass {
	height: calc(100vh - 170px);
	position: relative;
	overflow: hidden;
}
.accessDivClass .drawerClass .el-overlay {
	height: 100%;
	position: absolute !important;
	overflow: hidden;
	.el-drawer__body {
		overflow: hidden !important;
	}
}
</style>
